﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConfigMeister
{
    public partial class SettingsBox : Form
    {
        public SettingsBox()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void SettingsBox_Load(object sender, EventArgs e)
        {
            numericUpDown1.Value = SceneSettings.Width;
            numericUpDown2.Value = SceneSettings.Height;
            numericUpDown6.Value = SceneSettings.Fov;
            numericUpDown7.Value = SceneSettings.Clipping;
            comboBox2.SelectedIndex = SceneSettings.CameraType;
            comboBox1.SelectedIndex = SceneSettings.Antialising;
            numericUpDown3.Value = SceneSettings.AntiMultiplier;
            checkBox3.Checked = SceneSettings.Reflections;
            numericUpDown4.Value = SceneSettings.ReflectionsPrecision;
            checkBox4.Checked = SceneSettings.Refractions;
            numericUpDown5.Value = SceneSettings.RefractionDepth;
            checkBox6.Checked = SceneSettings.SceneDivision;
            comboBox3.SelectedIndex = SceneSettings.DivisionAlgorithm;
            numericUpDown8.Value = (decimal) SceneSettings.PixelAspect;
            checkBox1.Checked = SceneSettings.Lights;
            checkBox2.Checked = SceneSettings.Shades;
            checkBox5.Checked = SceneSettings.CornellBox;

            numericUpDown9.Value = (decimal) SceneSettings.CameraPos.X;
            numericUpDown10.Value = (decimal) SceneSettings.CameraPos.Y;
            numericUpDown11.Value = (decimal) SceneSettings.CameraPos.Z;

            numericUpDown14.Value = (decimal)SceneSettings.CameraTarget.X;
            numericUpDown13.Value = (decimal)SceneSettings.CameraTarget.Y;
            numericUpDown12.Value = (decimal)SceneSettings.CameraTarget.Z;

            radioButton1.Checked = (SceneSettings.CameraUp.X == 1);
            radioButton2.Checked = (SceneSettings.CameraUp.Y == 1);
            radioButton3.Checked = (SceneSettings.CameraUp.Z == 1);

            comboBox4.Items.AddRange(MaterialBucket.GetList());
            if (MaterialBucket.Count > 0)
            {
                if (SceneSettings.BgMaterial != "")
                {
                    comboBox4.SelectedItem = SceneSettings.BgMaterial;
                }
                else
                {
                    comboBox4.SelectedIndex = 0;
                }
                return;
            }
            comboBox4.Enabled = false;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SceneSettings.Width = (int) numericUpDown1.Value;
            SceneSettings.Height = (int) numericUpDown2.Value;
            SceneSettings.Fov = (int) numericUpDown6.Value;
            SceneSettings.Clipping = (int) numericUpDown7.Value;
            SceneSettings.CameraType = comboBox2.SelectedIndex;
            SceneSettings.Antialising = comboBox1.SelectedIndex;
            SceneSettings.AntiMultiplier = (int) numericUpDown3.Value;
            SceneSettings.Reflections = checkBox3.Checked;
            SceneSettings.ReflectionsPrecision = (int) numericUpDown4.Value;
            SceneSettings.Refractions = checkBox4.Checked;
            SceneSettings.RefractionDepth = (int) numericUpDown5.Value;
            SceneSettings.SceneDivision = checkBox6.Checked;
            SceneSettings.DivisionAlgorithm = comboBox3.SelectedIndex;
            SceneSettings.PixelAspect = (double) numericUpDown8.Value;
            SceneSettings.Lights = checkBox1.Checked;
            SceneSettings.Shades = checkBox2.Checked;
            SceneSettings.CornellBox = checkBox5.Checked;

            SceneSettings.CameraPos.X = (double) numericUpDown9.Value;
            SceneSettings.CameraPos.Y = (double) numericUpDown10.Value;
            SceneSettings.CameraPos.Z = (double) numericUpDown11.Value;

            SceneSettings.CameraTarget.X = (double) numericUpDown14.Value;
            SceneSettings.CameraTarget.Y = (double) numericUpDown13.Value;
            SceneSettings.CameraTarget.Z = (double) numericUpDown12.Value;

            if (radioButton1.Checked) SceneSettings.CameraUp = new Point3(1,0,0);
            else if (radioButton2.Checked) SceneSettings.CameraUp = new Point3(0,1,0);
            else if (radioButton3.Checked) SceneSettings.CameraUp = new Point3(0,0,1);


            if(MaterialBucket.Count == 0)
            {
                MessageBox.Show("Nie istnieją żadne materiały. Zostanie utworzony i przypisany domyślny materiał. Materiały możesz edytować w Edytorze materiałów.", "Brak materiału");
                Material temp = new Material();
                MaterialBucket.Add(temp);
                SceneSettings.BgMaterial = temp.Name;
            }
            else
            {
                SceneSettings.BgMaterial = comboBox4.SelectedItem.ToString();   
            }

            Close();
        }
    }
}
